<template>
  <div class="app-container">

    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="left" label-width="150px">

      <el-form-item label="方案类型">
        <el-radio-group v-model="postForm.uploadType" size="small">
          <el-radio :label="1">本地存储</el-radio>
          <el-radio :label="2">阿里云OSS</el-radio>
          <el-radio :label="3">七牛云</el-radio>
        </el-radio-group>
      </el-form-item>

      <div v-if="postForm.uploadType===1">
        <el-form-item label="本地目录">
          <el-input v-model="postForm.localUpload.localDir" placeholder="本地目录地址" />
        </el-form-item>
      </div>

      <div v-if="postForm.uploadType===2">
        <el-form-item label="EndPoint">
          <el-input v-model="postForm.ossUpload.endpoint" placeholder="访问存储节点，如：oss-cn-beijing.aliyuncs.com" />
        </el-form-item>

        <el-form-item label="Bucket">
          <el-input v-model="postForm.ossUpload.bucket" placeholder="阿里云存储容器" />
        </el-form-item>

        <el-form-item label="AccessKeyId">
          <el-input v-model="postForm.ossUpload.accessKeyId" placeholder="授权ID" />
        </el-form-item>

        <el-form-item label="AccessKeySecret">
          <el-input v-model="postForm.ossUpload.accessKeySecret" placeholder="授权秘钥" />
        </el-form-item>

        <el-form-item label="访问URL">
          <el-input v-model="postForm.ossUpload.url" placeholder="OSS访问域名或CDN加速域名，必须以/结尾，如：https://xxxx.oss-cn-beijing.aliyuncs.com/" />
        </el-form-item>

        <el-form-item label="智能媒体项目名称">
          <el-input v-model="postForm.ossUpload.project" placeholder="智能媒体项目名称" style="width: 300px" />  <small style="color: #555">用于文档转换 <a target="_blank" href="https://help.aliyun.com/document_detail/99384.html?spm=5176.8465980.0.dexternal.32ac1450VWbBrl">参考文档</a></small>
        </el-form-item>

        <el-form-item label="MPS管道ID">
          <el-input v-model="postForm.ossUpload.pipeline" placeholder="MPS管道ID" style="width: 300px" /> <small style="color: #555"> 用于视频转换 <a target="_blank" href="https://mps.console.aliyun.com/pipeline/list">参考文档</a> </small>
        </el-form-item>

      </div>

      <div v-if="postForm.uploadType===3">
        <el-form-item label="存储空间位置">
          <el-select v-model="postForm.qiniuUpload.endpoint">
            <el-option v-for="item in qiniuAreas" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="存储空间名称">
          <el-input v-model="postForm.qiniuUpload.bucket" placeholder="阿里云存储容器" />
        </el-form-item>

        <el-form-item label="AccessKeyId">
          <el-input v-model="postForm.qiniuUpload.accessKeyId" placeholder="授权ID" />
        </el-form-item>

        <el-form-item label="AccessKeySecret">
          <el-input v-model="postForm.qiniuUpload.accessKeySecret" placeholder="授权秘钥" />
        </el-form-item>

        <el-form-item label="访问URL">
          <el-input v-model="postForm.qiniuUpload.url" placeholder="外链域名，必须以/结尾，如：http://qozlc75sb.hn-bkt.clouddn.com/" />
        </el-form-item>

      </div>

      <el-row style="margin-top: 20px">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-row>

    </el-form>

  </div>
</template>

<script>
import { fetchDetail, saveData } from '@/api/sys/config/upload'

export default {
  name: 'UploadConfig',
  data() {
    return {

      qiniuAreas: [

        {
          label: '华东',
          value: 'https://upload.qiniup.com'
        },
        {
          label: '华北',
          value: 'https://upload-z1.qiniup.com'
        },
        {
          label: '华南',
          value: 'https://upload-z2.qiniup.com'
        },
        {
          label: '北美',
          value: 'https://upload-na0.qiniup.com'
        },
        {
          label: '东南亚',
          value: 'https://upload-as0.qiniup.com'
        }
      ],

      postForm: {
        uploadType: 1,
        ossUpload: {

        },
        localUpload: {
          localDir: ''
        }
      },
      loading: false,
      rules: {
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {

    fetchData() {
      fetchDetail().then(response => {
        this.postForm = response.data
      })
    },
    submitForm() {
      console.log(JSON.stringify(this.postForm))

      this.$refs.postForm.validate((valid) => {
        if (!valid) {
          return
        }

        this.loading = true
        this.postForm.id = '1'

        saveData(this.postForm).then(() => {
          this.$notify({
            title: '成功',
            message: '上传配置保存成功！',
            type: 'success',
            duration: 2000
          })
        })
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
